<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>

<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
	orient="vertical">

  <tabbox id="tabbox" flex="1">
    <tabs>
      <tab id="tab1" label="Tab 1" />
      <tab id="tab2" label="Tab 2" />
    </tabs>
    <tabpanels flex="1">
      <browser id="browser1" type="content-primary" flex="1" src="about:blank"/>
      <browser id="browser2" type="content-primary" flex="1" src="about:blank"/>
    </tabpanels>
  </tabbox>
  <script type="application/javascript" src="plugin-utils.js"/>
  <script type="application/javascript"><![CDATA[
    const ok = window.opener.wrappedJSObject.ok;
    const is = window.opener.wrappedJSObject.is;
    const done = window.opener.wrappedJSObject.done;
    const SimpleTest = window.opener.wrappedJSObject.SimpleTest;

    const nsIWebProgress = Components.interfaces.nsIWebProgress;
    const nsIWebProgressListener = Components.interfaces.nsIWebProgressListener;

    const kURI = 'http://mochi.test:8888/chrome/dom/plugins/test/mochitest/plugin_visibility_loader.html';

    function ProgressListener() {
    }
    ProgressListener.prototype.onStateChange =
      function(progress, req, flags, status) {
        if ((flags & nsIWebProgressListener.STATE_IS_WINDOW) &&
            (flags & nsIWebProgressListener.STATE_STOP))
          browserLoaded();
      };
    ProgressListener.prototype.QueryInterface = function(iid) {
      if (iid.equals(nsIWebProgressListener) ||
          iid.equals(Components.interfaces.nsISupportsWeakReference))
        return this;
      throw Components.results.NS_ERROR_NO_INTERFACE;
    };

    var loadCount = 0;
    function browserLoaded() {
      ++loadCount;
      if (2 == loadCount)
        startTest();
    }

    var tabbox = document.getElementById('tabbox');
    var browser1 = document.getElementById('browser1');
    var browser2 = document.getElementById('browser2');

    var progressListener1, progressListener2;

    function setup() {
      progressListener1 = new ProgressListener();
      browser1.addProgressListener(progressListener1, nsIWebProgress.NOTIFY_STATE_WINDOW);
      browser1.loadURI(kURI, null, null);
      progressListener2 = new ProgressListener();
      browser2.addProgressListener(progressListener2, nsIWebProgress.NOTIFY_STATE_WINDOW);
      browser2.loadURI(kURI, null, null);
    }

    window.addEventListener("load", setup, false);

    var plugin1, plugin2;

    const kTimeout = 5000; // 5 seconds
    var paintGiveUp;
    var paintInterval;

    function startTest() {
      plugin1 = browser1.contentDocument.getElementById('p').wrappedJSObject;
      plugin2 = browser2.contentDocument.getElementById('p').wrappedJSObject;

      paintGiveUp = Date.now() + kTimeout;
      paintInterval = setInterval(waitForPaint, 100);
    }

    function waitForPaint() {
      if (!plugin1.isVisible()) {
        if (Date.now() < paintGiveUp)
          return;

        ok(false, "Plugin in tab 1 never became visible.");
        done();
        return;
      }

      clearInterval(paintInterval);

      ok(true, "Plugin in tab 1 should be visible.");
      paintCountIs(plugin1, 1, "Plugin in tab 1 should have painted once.");

      ok(!plugin2.isVisible(), "Plugin in tab 2 should not be visible.");
      paintCountIs(plugin2, 0, "Plugin in tab 2 should not have painted.");

      tabbox.selectedIndex = 1;
      paintGiveUp = Date.now() + kTimeout;
      paintInterval = setInterval(part2, 100);
    }

    function part2() {
      if (!plugin2.isVisible()) {
        if (Date.now() < paintGiveUp)
          return;

        ok(false, "Plugin in tab 2 never became visible.");
        done();
        return;
      }

      clearInterval(paintInterval);

      ok(true, "Plugin in tab 2 became visible.");
      paintCountIs(plugin2, 1, "Plugin in tab 2 should have painted once.");

      ok(!plugin1.isVisible(), "Plugin in tab 1 should have become invisible.");
      paintCountIs(plugin1, 1, "Plugin in tab 1 should have painted once.");

      // Setcolor invalidates
      plugin1.setColor('FF00FF00');
      plugin2.setColor('FF00FF00');

      setTimeout(part3, 500);
    }

    function part3() {
      paintCountIs(plugin1, 1,
                   "Plugin in tab 1 should not have repainted after invalidate.");
      paintCountIs(plugin2, 2,
                   "Plugin in tab 2 should have repainted after invalidate.");
      done();
    }
  ]]></script>

</window>
